package com.opennews.openplatform.dataaggregatorreactiveservice.service

import kotlinx.coroutines.reactor.awaitSingle
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.util.UriComponentsBuilder

@Service
class ProductService {
    private val webClient: WebClient = WebClient.create()

    suspend fun query(ids: List<String>): List<Map<String, Any>> {
        val uri = UriComponentsBuilder.fromUriString("lb://product-service/api/products")
            .queryParam("ids", ids)
            .toUriString()

        return webClient.get()
            .uri(uri)
            .retrieve()
            .bodyToMono(List::class.java)
            .awaitSingle()
            .map { it as Map<String, Any> }
    }
}